package com.th.controller;

import com.th.bean.Employye;
import com.th.service.EmployyeService;
import com.th.util.ErrorBindingResult;
import com.th.util.MyJson;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/em")
public class EmployeeController {
    @Resource
    private EmployyeService employyeService;

    /**
     * 人员查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "",method = RequestMethod.GET)
    public MyJson findAll(){
        return MyJson.ok(employyeService.findAll());
}

    /**
     * 删除人员
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public MyJson delete(@PathVariable(value = "id") Integer id){
            int row=employyeService.delete(id);
            if(row>0) {
                return MyJson.ook("删除成功");
            }else {
                return MyJson.ook("删除失败");
            }

    }
    /**
     * 添加人员
     * @param employye
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "",method = RequestMethod.POST)
    public MyJson insert(@Validated Employye employye, BindingResult result) {
            int i=8/0;
            MyJson myJson= ErrorBindingResult.returnError(result);
           if (myJson!=null&&!myJson.getMsg().equals("")){
            return myJson;
           }
            int row=employyeService.insert(employye);
            if(row>0){
                return MyJson.ook("添加成功");
            }else {
                return MyJson.ook("添加失败");
            }


    }
    /**
     * 修改人员
     * @param employye
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "",method = RequestMethod.PUT)
    public MyJson update(@Validated Employye employye, BindingResult result){
        if(employye.getId()==null){
            return MyJson.ook("没有传入id");
        }
        MyJson myJson= ErrorBindingResult.returnError(result);
        if (myJson!=null||!myJson.getMsg().equals("")){
            return myJson;
        }
            int row=employyeService.update(employye);
            if(row>0){
                return MyJson.ook("修改成功");
            }else {
                return MyJson.ook("修改失败");
            }


    }

}
